<template>
  <div class="loginBody">
    <div class="loginDiv" >
      <div class="login-content">
        <h1 class="login-title">用户登录</h1>
        <el-form :model="loginForm" label-width="100px"
                 :rules="rules" ref="loginForm">
          <el-form-item label="账号：" prop="username">
            <el-input style="width: 200px" type="text" v-model="loginForm.username"
                      autocomplete="off" size="small"></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input style="width: 200px" type="password" v-model="loginForm.password"
                      show-password autocomplete="off" size="small" @keyup.enter.native="confirm"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="confirm" :disabled="confirm_disabled">登录</el-button>
            <el-button type="primary" @click="resetForm" style="margin-left: 60px">重置</el-button>
            <p>还没账号？<el-button type="text" @click="register">注册</el-button></p>
          </el-form-item>
        </el-form>

        <div>
          <el-dialog
              title="注册账号"
              :visible.sync="centerDialogVisible"
              width="40%"
              center>
            <el-form :rules="rules" ref="form" :model="form" label-width="200px">
              <el-form-item label="用户名：" prop="name">
                <el-col :span="25">
                  <el-input v-model="form.name"></el-input>
                </el-col>
              </el-form-item>
              <el-form-item label="账号：" prop="username">
                <el-col :span="25">
                  <el-input v-model="form.username"></el-input>
                </el-col>
              </el-form-item>
              <el-form-item label="密码：" prop="password">
                <el-col :span="25">
                  <el-input v-model="form.password"></el-input>
                </el-col>
              </el-form-item>
              <el-form-item label="性别">
                <el-radio-group v-model="form.sex">
                  <el-radio label="1">男</el-radio>
                  <el-radio label="2">女</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="年龄：" prop="age">
                <el-col :span="25">
                  <el-input v-model="form.age"></el-input>
                </el-col>
              </el-form-item>
              <el-form-item label="手机：" prop="phone">
                <el-col :span="25">
                  <el-input v-model="form.phone"></el-input>
                </el-col>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doRegister">确 定</el-button>
    </span>
          </el-dialog>
        </div>

      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    let checkAge = (rule, value, callback) => {
      if (value > 150) {
        callback(new Error('年龄输入过大'))
      } else {
        callback();
      }
    };
    let checkDuplicate = (rule, value, callback) => {
      if (this.form.id) {
        return callback();
      }
      this.$axios.get("http://localhost:8090/user/findByUsername?username=" + this.form.username).then(res => res.data).then(res => {
        if (res.code != 200) {
          callback()
        } else {
          callback(new Error('账号已经存在'))
        }
      })
    }
    return {
      confirm_disabled: false,
      centerDialogVisible: false,
      loginForm: {
        username: '',
        password: ''
      },
      form: {
        id: '',
        name: '',
        username: '',
        password: '',
        age: '',
        phone: '',
        sex: '',
        roleId: '2',
        isvalid: 'y',
      },
      rules: {
        name: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur'},
          {validator: checkDuplicate, trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        age: [
          {required: true, message: '请选择活动区域', trigger: 'change'},
          {min: 1, max: 3, message: '长度在 1 到 3 个字符', trigger: 'blur'},
          {pattern: /^([1-9][0-9]*){1,3}$/, message: '年龄必须为正整数', trigger: 'blur'},
          {validator: checkAge, trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '手机号不能为空', trigger: 'blur'},
          {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: '请输入正确的手机号码', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    doRegister() {
      this.$axios.post(this.$httpUrl + '/register', this.form).then(res => res.data).then(res => {
        if (res.code == 200) {
          this.$message({
            showClose: true,
            message: '用户注册成功',
            type: 'success'
          });
          this.centerDialogVisible = false;
          this.resetForm();
          this.resetResform();
        } else {
          this.$message({
            showClose: true,
            message: '用户注册失败',
            type: 'error'
          });
        }
      })
    },
    register(){
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.resetResform();
      })
    },
    resetForm() {
      this.loginForm.username = ''
      this.loginForm.password = ''
    },
    resetResform(){
      this.$refs.form.resetFields();
    },
    confirm() {
      this.confirm_disabled = true
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$axios.post(this.$httpUrl + '/login', this.loginForm).then(res => res.data).then(res => {
            if (res.code == 200) {
              //存储
              sessionStorage.setItem("CurUser", JSON.stringify(res.data.user))
              // sessionStorage.setItem("menuList",JSON.stringify(res.data.menu))
              this.$store.commit("setMenu", res.data.menu)
              //跳转主页
              this.$router.replace('/Index');
            } else {
              this.confirm_disabled = false;
              alert('校验失败，账号或密码错误！');
              return false;
            }
          });
        } else {
          this.confirm_disabled = false;
          console.log('校验失败')
          return false;
        }
      })
    }
  },
}
</script>

<style scoped>
.loginBody {
  position: absolute;
  width: 100%;
  height: 100%;
  /*渐变背景色*/
  background-image: linear-gradient(to bottom right, #FC466B, #3F5EF8);
  overflow: hidden;
}

.loginDiv {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;
  margin-left: -250px;
  width: 450px;
  height: 330px;
  background-color: #fff;
  background-image: linear-gradient(to bottom left, #ffa114, #73fd0f);
  border-radius: 5%;
}
.login-title {
  margin: 20px 0;
  text-align: center;
  text-shadow: 5px 5px 5px #00ffe6;
}

.login-content {
  width: 400px;
  height: 250px;
  position: absolute;
  top: 25px;
  left: 25px;
}

</style>